import java.io.*;

class Role implements Serializable {
    private String name;
    private  int hp;
    private  int level;
    private String weapon;
    public void beatEnemy(){
        System.out.println(this.name+"在和敌人作战！");
    }

    public Role(String name, int hp, int level, String weapon) {
        this.name = name;
        this.hp = hp;
        this.level = level;
        this.weapon = weapon;
    }

    @Override
    public String toString() {
        return "Role{" +
                "name='" + name + '\'' +
                ", hp=" + hp +
                ", level=" + level +
                ", weapon='" + weapon + '\'' +
                '}';
    }
}
class Map implements Serializable{
    private  int x;
    private  int y;
    private String scene;
    public void showScene(){
        System.out.println("地图中出现"+this.scene+"的美丽风景！");
    }

    @Override
    public String toString() {
        return "Map{" +
                "x=" + x +
                ", y=" + y +
                ", scene='" + scene + '\'' +
                '}';
    }

    public Map(int x, int y, String scene) {
        this.x = x;
        this.y = y;
        this.scene = scene;

        }
    }

public class 对象流 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        将对象写入文件();
    }

    private static void 将对象写入文件() throws IOException, ClassNotFoundException {
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("day5//1.dat"));

        try{
            while (true){
                Object obj=ois.readObject();
                if(obj instanceof Role){
                    ((Role) obj).beatEnemy();
                }else if(obj instanceof Map){
                    ((Map)obj).showScene();
                }
                System.out.println(obj);
            }
        }catch(EOFException e){
            System.out.println("读取结束");
        }
        ois.close();


        Role r1=new Role("李逍遥",50,10,"仙剑");
        Role r2=new Role("重楼",30,80,"魔戟");
        Role r3=new Role("拜月教主",5000,100,"无");
        Map map=new Map(10,20,"仙乐岛");
        //写入
//        oos.writeObject(r1);
//        oos.writeObject(r2);
//        oos.writeObject(r3);
//        oos.writeObject(map);
//        oos.close();
        System.out.println("写入成功");
    }
}
